package com.yushutao.menu;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yushutao.menu.beans.DishBean;
import com.yushutao.menu.beans.OrderBean;
import com.yushutao.menu.beans.UserBean;
import com.yushutao.menu.dao.IDish;
import com.yushutao.menu.dao.IOrder;
import com.yushutao.menu.dao.IUser;
import com.yushutao.menu.dao.impl.DishDAO;
import com.yushutao.menu.dao.impl.OrderDAO;
import com.yushutao.menu.dao.impl.UserDAO;

@Controller
@RequestMapping(value = "user")
public class UserController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	private static final IDish dishDAO = new DishDAO();
	private static final IUser userDAO = new UserDAO();
	private static final IOrder orderDAO = new OrderDAO();
	
	/**
	 * 显示主页
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String showIndex(Model model) {
		logger.debug("req showIndex");
		List<DishBean> all = dishDAO.getAllDish();
		model.addAttribute("dishes", all);
		return "user/index";
	}
	
	/**
	 * 用户填写个人信息
	 */
	@RequestMapping(value = "/showUserReg", method = RequestMethod.GET)
	public String showUserReg(HttpServletRequest request,Model model){
		logger.debug("req showUserReg");
		if(request.getSession().getAttribute(DataValue.USER_IN_SESSION_KEYNAME) == null){
			return "user/userReg";			
		}else{
			model.addAttribute(DataValue.ERROR_MESSAGE_NAME,"请勿重复注册");
			return "error";
		}
	}
	
	/**
	 * 用户填写外卖信息
	 */
	@RequestMapping(value = "/showOuterSell", method = RequestMethod.GET)
	public String showOuterSell(Model model){
		logger.debug("req showOuterSell");
		return "user/outerSell";
	}
	
	/**
	 * 服务器接受用户填写信息
	 */
	@RequestMapping(value = "/doUserReg", method = RequestMethod.GET)
	public String doUserReg(HttpServletRequest request,UserBean user){
		logger.debug("req doUserReg");
		UserBean check = userDAO.getUserByPhoneAndDesk(user.getPhoneNumber(),user.getDeskNumber());
		if(check == null){
			userDAO.addUser(user);
			request.getSession().setAttribute(DataValue.USER_IN_SESSION_KEYNAME,user);
		}else{
			request.getSession().setAttribute(DataValue.USER_IN_SESSION_KEYNAME,check);
		}
		return "redirect:/user/showOrderMenu";
	}
	
	/**
	 * 用户找回自己的信息
	 */
	@RequestMapping(value = "/doUserRemem", method = RequestMethod.GET)
	public String doUserRemem(HttpServletRequest request,UserBean user){
		logger.debug("req doUserReg");
		user = userDAO.getUserByPhoneAndDesk(user.getPhoneNumber(),user.getDeskNumber());
		request.getSession().setAttribute(DataValue.USER_IN_SESSION_KEYNAME,user);
		return "redirect:/user/showOrderMenu";
	}
	
	/**
	 * 显示找回信息页面
	 */
	@RequestMapping(value = "/showUserRemem", method = RequestMethod.GET)
	public String showUserRemem(HttpServletRequest request){
		logger.debug("req doUserReg");
		UserBean user = (UserBean)request.getSession().getAttribute(DataValue.USER_IN_SESSION_KEYNAME);
		if(user != null){
			return "redirect:/user/showOrderMenu";			
		}else{
			return "user/userRemem";
		}
	}
	
	/**
	 * 进入点餐状态
	 */
	@RequestMapping(value = "/showOrderMenu", method = RequestMethod.GET)
	public String showOrderMenu(HttpServletRequest request,Model model){
		logger.debug("req showOrderMenu");
		UserBean user;
		if((user = getUser(request)) != null){
			model.addAttribute("userBean",user);
			model.addAttribute("dishes",dishDAO.getAllDish());
			List<OrderBean> ordered = orderDAO.getOrderByUserId(user.getId());
			model.addAttribute("ordered",ordered);
			String payFlag = "yes";
			double allCount = 0d;
			for(OrderBean ob:ordered){
				allCount+=ob.getPrize();
				if("no".equals(ob.getIsCookied())){
					payFlag = "no";
				}
			}
			model.addAttribute("canPayFlag",payFlag);
			model.addAttribute("allCount",allCount);
			return "user/orderMenu";
		}else{
			return "redirect:/user/showUserReg";
		}
	}
	
	/**
	 * 处理点餐(临时的，可以再删除的)
	 */
	@RequestMapping(value = "/doOrderMenu/{pCode}", method = RequestMethod.GET)
	public String doOrderMenu(@PathVariable("pCode")int pCode,HttpServletRequest request,Model model){
		logger.debug("req doOrderMenu");
		UserBean user = (UserBean)request.getSession().getAttribute(DataValue.USER_IN_SESSION_KEYNAME);
		if(user != null){
			OrderBean ob = new OrderBean();
			ob.setDishId(pCode);
			ob.setUserId(user.getId());
			orderDAO.addOrder(ob);
			return "redirect:/user/showOrderMenu";
		}else{
			model.addAttribute(DataValue.ERROR_MESSAGE_NAME,"操作超时");
			return "error";
		}
	}
	
	/**
	 * 处理点餐(临时的，可以再删除的)
	 */
	@RequestMapping(value = "/doDeleteMenu/{oCode}", method = RequestMethod.GET)
	public String doDeleteMenu(@PathVariable("oCode")int oCode,HttpServletRequest request,Model model){
		logger.debug("req doDeleteMenu");
		UserBean user = (UserBean)request.getSession().getAttribute(DataValue.USER_IN_SESSION_KEYNAME);
		if(user != null){
			orderDAO.removeOrder(oCode,user.getId());
			return "redirect:/user/showOrderMenu";
		}else{
			model.addAttribute(DataValue.ERROR_MESSAGE_NAME,"操作超时");
			return "error";
		}
	}
	
	/**
	 * 处理点餐(确定的，不可删除的)
	 */
	@RequestMapping(value = "/doConfirmMenu", method = RequestMethod.GET)
	public String doConfirmMenu(HttpServletRequest request,Model model){
		logger.debug("req doConfirmMenu");
		UserBean user = (UserBean)request.getSession().getAttribute(DataValue.USER_IN_SESSION_KEYNAME);
		if(user != null){
			orderDAO.confirmOrderByUserId(user.getId());
			return "redirect:/user/showOrderMenu";
		}else{
			model.addAttribute(DataValue.ERROR_MESSAGE_NAME,"操作超时");
			return "error";
		}
	}
	
	/**
	 *结账
	 */
	@RequestMapping(value = "/doPayMenu", method = RequestMethod.GET)
	public String doPayMenu(HttpServletRequest request,Model model){
		logger.debug("req doPayMenu");
		UserBean user = (UserBean)request.getSession().getAttribute(DataValue.USER_IN_SESSION_KEYNAME);
		if(user != null){
			userDAO.payUser(user.getId());
			return "redirect:/user/showOrderMenu";
		}else{
			model.addAttribute(DataValue.ERROR_MESSAGE_NAME,"操作超时");
			return "error";
		}
	}
	private UserBean getUser(HttpServletRequest request){
		UserBean res = (UserBean)request.getSession().getAttribute(DataValue.USER_IN_SESSION_KEYNAME);
		if(res != null){
			res = userDAO.getUserById(res.getId());
			if(res != null){
				request.getSession().setAttribute(DataValue.USER_IN_SESSION_KEYNAME,res);				
			}else{
				request.getSession().removeAttribute(DataValue.USER_IN_SESSION_KEYNAME);
			}
		}
		return res;
	}
}
